#include <string.h>
#include <iostream>
using std::cout;
using std::endl;


class String
{
public:
    String()
    /* : _pstr(nullptr) */
    : _pstr(new char[1]())
    {
        cout << "String()" << endl;
    }

    String(const char *pstr)
    : _pstr(new char[strlen(pstr) + 1]())
    {
        cout << "String(const char *)" << endl;
        strcpy(_pstr, pstr);
    }

    String(const String & rhs)
    : _pstr(new char[strlen(rhs._pstr) + 1]())
    {
        cout << "String(const String &)" << endl;
        strcpy(_pstr, rhs._pstr);
    }

    String & operator=(const String & rhs)
    {
        cout << "String &operator=(const String &)" << endl;
        if(this != &rhs)
        {
            if(_pstr)
            {
                delete [] _pstr;
            }
            _pstr = new char[strlen(rhs._pstr) + 1]();
            strcpy(_pstr, rhs._pstr);
        }
        return *this;
    }

    size_t length() const
    {
        size_t len = 0;
        if(_pstr)
        {
            len = strlen(_pstr);
        }

        return len;

    }

    const char * c_str() const
    {
        if(_pstr)
        {
            return _pstr;
        }
        else
        {
            return nullptr;
        }
    }


    ~String()
    {
        cout << "~String()" << endl;
        if(_pstr)
        {
            delete [] _pstr;
            _pstr = nullptr;
        }
    }

    void print() const
    {
        if(_pstr)
        {
            cout << "_pstr = " << _pstr << endl;
        }else{
            cout << endl;
        }
    }

private:
    char * _pstr;
};


String && func(){
    return String("hello");
}

void test1(){
    int a = 1, b = 2;
    int && ref = a + b;
    cout << &ref << endl;

    String && ref2 = func();
    cout << "111111111" << endl;
}


/* int && func(int a,int b){ */
/*  return a + b; */
/* } */

/* void test2(){ */
/*  // &func(1,2);  //无法取址 */
/*  int && ref = func(1,2); */
/*  cout << &ref << endl; */
/* } */

void test3(){
    /* const String & ref = String("hello"); */
    String && ref = String("hello");
    cout << "111111111" << endl;
}


int main(void){
    test1();
    cout << "2222222" << endl;
    return 0;
}
